
import React, { createContext, useContext, useState, useEffect } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';

interface PayContextType {
    item: any;
    total: number;
    list: any[];
    list2: any[];
    list2_one : any;
    setItem: (item: any) => void;
    setTotal: (total: number) => void;
    setList: (list: any[]) => void;
    setList2: (list: any[]) => void;
    setList2_one: (item: any) => void;
    clearData: () => void; // 添加清除数据的函数
}

const PayContext = createContext<PayContextType | undefined>(undefined);

export const usePayContext = () => {
    const context = useContext(PayContext);
    if (!context) {
        throw new Error('usePayContext must be used within a PayProvider');
    }
    return context;
};

export const PayProvider: React.FC = ({ children }) => {
    const [item, setItem] = useState<any>({});
    const [total, setTotal] = useState<number>(0);
    const [list, setList] = useState<any[]>([]);
    const [list2, setList2] = useState<any[]>([]);
    const [list2_one, setList2_one] = useState<any>({});

    // Load data from AsyncStorage on component mount
    useEffect(() => {
        const loadData = async () => {
            try {
                const storedItem = await AsyncStorage.getItem('@item');
                const storedTotal = await AsyncStorage.getItem('@total');
                const storedList = await AsyncStorage.getItem('@list');
                const storedList2 = await AsyncStorage.getItem('@list2');
                const storedList2_one = await AsyncStorage.getItem('@list2_one');

                if (storedItem !== null) {
                    setItem(JSON.parse(storedItem));
                }
                if (storedTotal !== null) {
                    setTotal(JSON.parse(storedTotal));
                }
                if (storedList !== null) {
                    setList(JSON.parse(storedList));
                }
                if (storedList2 !== null) {
                    setList2(JSON.parse(storedList2));
                }
                if (storedList2_one !== null) {
                    setList2_one(JSON.parse(storedList2_one));
                }
            } catch (error) {
                console.error('Error loading data from AsyncStorage:', error);
            }
        };

        loadData();
    }, []);

    // Save data to AsyncStorage whenever item, total, or list changes
    useEffect(() => {
        const saveData = async () => {
            try {
                await AsyncStorage.setItem('@item', JSON.stringify(item));
                await AsyncStorage.setItem('@total', JSON.stringify(total));
                await AsyncStorage.setItem('@list', JSON.stringify(list));
                await AsyncStorage.setItem('@list2', JSON.stringify(list2));
                await AsyncStorage.setItem('@list2', JSON.stringify(list2_one));
            } catch (error) {
                console.error('Error saving data to AsyncStorage:', error);
            }
        };

        saveData();
    }, [item, total, list]);

    // Clear data from AsyncStorage
    const clearData = async () => {
        try {
            await AsyncStorage.removeItem('@list');
            setList([]);
        } catch (error) {
            console.error('Error clearing data from AsyncStorage:', error);
        }
    };

    return (
        <PayContext.Provider value={{ 
            item, total, list, list2,list2_one,
            setItem, setTotal, setList, setList2,setList2_one,
            clearData 
            }}>
            {children}
        </PayContext.Provider>
    );
};